package com.joe.demo.chat.protocol;

import com.joe.demo.chat.message.LoginRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Description
 * @Author gaojw
 * @Date 2024-08-30
 */
public class TestMessageCodec {

    public static void main(String[] args) throws Exception {
        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
                new LoggingHandler(),
                new LengthFieldBasedFrameDecoder(1024, 12, 4, 0, 0),
                new MessageCodecSharable()
        );
        final LoginRequestMessage loginRequestMessage = new LoginRequestMessage("zhangsan", "123", "张三");
        // 出
        // embeddedChannel.writeOutbound(loginRequestMessage);


        final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, loginRequestMessage, buffer);

        final ByteBuf s1 = buffer.slice(0, 100);
        final ByteBuf s2 = buffer.slice(100, buffer.readableBytes() - 100);
        // 入
        s1.retain();
        embeddedChannel.writeInbound(s1);
        embeddedChannel.writeInbound(s2);
    }
}
